﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace FlexControls
{
    public partial class fcListView : UserControl
    {
       
        public string strTextBoxCode = "";
        public string strProc="";
        public string strTableName = "";
        public string strSelectItemCode = "", strSelectItemData="";

        public ADODB.Connection Conn;
        public new event EventHandler DoubleClick;
        public new event EventHandler MouseDoubleClick;
        public new event EventHandler ItemChecked;
        public int eX = 0;
        public int eY = 0;
        public int eWidth = 0;
        public int eHeight = 0;
        public int eIndex = 0;
        protected virtual void OnMouseDoubleClick()
        {
            if (this.MouseDoubleClick != null)
            {
                this.MouseDoubleClick(this, EventArgs.Empty);

            }
        }
        protected virtual void OnItemChecked()
        {
            if (this.ItemChecked != null)
            {
                this.ItemChecked(this, EventArgs.Empty);

            }
        }
        protected virtual void OnDoubleClick()
        {
            if (this.DoubleClick != null)
            {
                this.DoubleClick(this, EventArgs.Empty);

            }
        }

        public fcListView()
        {
            InitializeComponent();
         
        }

        public string fcProc
        {
            get
            {
                return strProc;
            }
            set
            {
                strProc = value;
            }
        }

        public string fcTableName
        {
            get
            {
                return strTableName;
            }
            set
            {
                strTableName = value;
            }
        }
        public void LoadData(string strSQL,ADODB.Connection fcConn)
        {
            try
            {
                ADODB.Recordset Rs = new ADODB.Recordset();

                Rs.Open(strSQL, fcConn,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockReadOnly,-1);
                lsv.Clear();
                if (Rs.RecordCount > 0)
                {
                    if (booShowTitle == true)
                    {
                        //Thêm tên cột listview

                        
                        // xtr.DataAdapter = this.proc_load_baocaotongketchitietTableAdapter;

                        
                        ColumnHeader column = new ColumnHeader();
                        column.Width = 50;
                        column.Text = "STT";

                        lsv.Columns.Add(column);
                        cmbSort.Items.Clear();
                        for (int j = 1; j < Rs.Fields.Count; j++)
                        {
                            column = new ColumnHeader();
                            column.Width = 100;
                            column.Text = Rs.Fields[j].Name;
                            cmbSort.Items.Add(Rs.Fields[j].Name);
                            lsv.Columns.Add(column);
                        }
                    }
                    //Thêm dữ liệu vào listview
                    ListViewItem lsvItemSelect = new ListViewItem();
                    if (strSelectItemCode != "")
                    {
                        
                        lsv.Items.Add(lsvItemSelect);
                        lsvItemSelect.Tag = strSelectItemCode;
                    }
                    for (int i = 0; i < Rs.RecordCount; i++)
                    {
                        if (Rs.Fields[0].Value == strSelectItemCode)
                        {
                            for (int j = 1; j < Rs.Fields.Count; j++)
                            {
                                lsvItemSelect.Text = Convert.ToString(i + 1);
                                lsvItemSelect.SubItems.Add(Convert.ToString(Rs.Fields[j].Value));
                            }
                        }
                        else
                        {
                            ListViewItem lsvItem = new ListViewItem();
                            lsv.Items.Add(lsvItem);
                            lsvItem.Tag = Rs.Fields[0].Value;
                            for (int j = 1; j < Rs.Fields.Count; j++)
                            {
                                lsvItem.Text = Convert.ToString(i + 1);
                                lsvItem.SubItems.Add(Convert.ToString(Rs.Fields[j].Value));
                            }
                            
                           
                        }
                        Rs.MoveNext();
                    }
                    lsv.Items[0].Selected = true;
                    
                   
                }
                Rs.Close();

            }
            catch (Exception ex)
            {

                MessageBox.Show("Không thể load dữ liệu cho listview" + Convert.ToString(ex));
            }
        }


        private void fcListView_SizeChanged(object sender, EventArgs e)
        {

            if (booTextbox == true)
            {
                
            }
            else
            {
                lsv.Location.Y.Equals(3);
                
            }
            if (booSort == true && booTextbox==true)
            {
                
                if(this.Width<482)
                    this.Width = 482;
            }
            else if(booSort==true || booTextbox==true)
            {
                if(this.Width<262)
                this.Width = 262;
                cmbSort.Location.Y.Equals(3);
                label1.Location.Y.Equals(3); 
            }
            if (booSort == true)
            { 
                cmbSort.Location.Y.Equals(3);
                label1.Location.Y.Equals(3); 
            }
          //  label1.Location = new Point();
        }

        private void fcListView_Load(object sender, EventArgs e)
        {
            fctxtBox.txtConn = Conn;
        }

        private void lsv_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (booCheckBox == false)
                {
                    if (fcShowTitle == true)
                    {
                        strSelectItemData = lsv.SelectedItems[0].SubItems[1].Text;
                    }
                    strSelectItemCode = Convert.ToString(lsv.SelectedItems[0].Tag);
                    ListView.SelectedListViewItemCollection items = lsv.SelectedItems;
                    foreach (ListViewItem item in items)
                    {


                        //strSelectItemData=Convert.ToString(item
                    }
                    OnDoubleClick();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Listview double click" + Convert.ToString(ex));
            }
            
        }

        private void lsv_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                
                ListView.SelectedListViewItemCollection items = lsv.SelectedItems;
                foreach (ListViewItem item in items)
                {
                    if (fcShowTitle == true)
                    {
                        strSelectItemData = lsv.SelectedItems[0].SubItems[1].Text;
                    }
                    strSelectItemCode = Convert.ToString(item.Tag);
                }

                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Selected Index Changed" + Convert.ToString(ex));
            }
        }
        public bool booTextbox = true;
        public bool fcTextBox
        {
            get
            {
                return booTextbox;
            }
            set
            {
                booTextbox = value;
                if (booTextbox == true )
                {
                    fctxtBox.Visible = true;
                    
                    lsv.Location=new Point(2,23);

                }
                else if (booTextbox == false && booSort == false)
                {
                    fctxtBox.Visible = false;
                    lsv.Location = new Point(3, 3);
                    lsv.Height = this.Size.Height - 5;
                }
                else 
                {
                    fctxtBox.Visible = false;
                }
            }
        }
        public string strTextboxProc = "";
        public string fcTextboxProc
        {
            get
            {
                return strTextboxProc;
            }
            set
            {
                strTextboxProc = value;
                fctxtBox.fcProc_ListView = strTextboxProc;

            }
        }
        public string strTextboxFieldName = "";
        public string fcTextboxFieldName
        {
            get
            {
                return strTextboxFieldName;
            }
            set
            {
                strTextboxFieldName = value;
                fctxtBox.fcFieldName = strTextboxFieldName;
            }
        }
        public int intTexboxVarNum = 0;
        public int fcTextboxVarNum
        {
            get
            {
                return intTexboxVarNum;
            }
            set
            {
                intTexboxVarNum = value;
            }
        }
        public void LoadDataFromTextBox()
        {
            try
            {
                if (booCheckBox == false)
                {
                    string strCode = fctxtBox.strCode;
                    string strValue = Convert.ToString(strProc);
                    char[] delimiters = new char[] { ' ' };
                    string[] strSplit = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    string strSQL = "exec " + strSplit[0] + " '" + strCode + "'";
                    LoadData(strSQL, Conn);
                }
                else
                {
                    string strCode = fctxtBox.strCode;
                    string strValue = Convert.ToString(strProcCheck);
                    char[] delimiters = new char[] { ' ' };
                    string[] strSplit = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                    string strSQLCheck = "exec " + strSplit[0] + " '" + strCode + "'";
                    strValue = Convert.ToString(strProcUnCheck);
                    delimiters = new char[] { ' ' };
                    strSplit = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    string strSQLUnCheck = "exec " + strSplit[0];
                    LoadDataWithCheckBox(strSQLCheck, strSQLUnCheck, Conn);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Load Data From TextBox\n" + ex.ToString());
            }
        }
        public void LoadDataWithCheckBox(string strSQLCheck, string strSQLUnCheck,ADODB.Connection fcConn)
        {
            try 
            {
                
                ADODB.Recordset RsUnCheck = new ADODB.Recordset();
                RsUnCheck.Open(strSQLUnCheck, fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, -1);
                ADODB.Recordset RsCheck = new ADODB.Recordset();
                RsCheck.Open(strSQLCheck, fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly,-1);

                if (RsUnCheck.RecordCount > 0)
                {
                    lsv.Clear();
                    if (booShowTitle == true)
                    {
                        //Thêm tên cột listview
                        ColumnHeader column = new ColumnHeader();
                        column.Width = 50;
                        column.Text = "STT";

                        lsv.Columns.Add(column);
                        for (int j = 1; j < RsUnCheck.Fields.Count; j++)
                        {
                            column = new ColumnHeader();
                            column.Width = 100;
                            column.Text = RsUnCheck.Fields[j].Name;
                            lsv.Columns.Add(column);
                        }
                    }
                    //Thêm dữ liệu vào listview

                    for (int i = 0; i < RsUnCheck.RecordCount; i++)
                    {
                        ListViewItem lsvItem = new ListViewItem();
                        lsv.Items.Add(lsvItem);
                        lsvItem.Tag = RsUnCheck.Fields[0].Value;
                        if (fcShowTitle == true)
                        {
                            for (int j = 1; j < RsUnCheck.Fields.Count; j++)
                            {
                                
                                    lsvItem.Text = Convert.ToString(i + 1);
                                    lsvItem.SubItems.Add(Convert.ToString(RsUnCheck.Fields[j].Value));
                               

                            }
                        }
                                      else
                                {
                                    lsvItem.Text = Convert.ToString(RsUnCheck.Fields[1].Value);
                                }
                        lsvItem.Checked = false;
                        
                        if (RsCheck.RecordCount > 0)
                        {
                            for (int k = 0; k < RsCheck.RecordCount; k++)
                            {
                                string strValue = RsUnCheck.Fields[0].Value;
                                char[] delimiters = new char[] { ' ' };
                                string[] strSplit = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                                if (strSplit[0] == RsCheck.Fields[0].Value)
                                {
                                    lsvItem.Checked = true;
                                    
                                }
                                RsCheck.MoveNext();
                            }
                            RsCheck.MoveFirst();
                        }
                        RsUnCheck.MoveNext();
                    }
                    lsv.Items[0].Selected = true;


                }
                RsUnCheck.Close();
                           }
            catch(Exception ex)
            {
                MessageBox.Show("Load Data With CheckBox" + Convert.ToString(ex));
            }
        }

        private void fctxtBox_DoubleClick(object sender, EventArgs e)
        {
            LoadDataFromTextBox();
            strTextBoxCode = fctxtBox.strCode;
            this.Parent.Focus();
        }
        public bool booCheckBox = false;
        public bool fcCheckBox
        {
            get
            {
                return booCheckBox;
            }
            set
            {
                booCheckBox = value;
                if (booCheckBox == true)
                {
                    lsv.CheckBoxes = true;

                }
                else
                {
                    lsv.CheckBoxes = false;
                }
            }
        }
        public string strProcCheck = "";
        public string fcProcCheck
        {
            get
            {
                return strProcCheck;
            }
            set
            {
                strProcCheck = value;
            }
        }
        public string strProcUnCheck = "";
        public string fcProcUnCheck
        {
            get
            {
                return strProcUnCheck;
            }
            set
            {
                strProcUnCheck = value;
            }
        }

        private void lsv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (booCheckBox == false)
                {
                    eX = lsv.SelectedItems[0].GetSubItemAt(e.X, e.Y).Bounds.Left + 3;
                    eY = lsv.SelectedItems[0].GetSubItemAt(e.X, e.Y).Bounds.Top + 23;
                    eWidth = lsv.SelectedItems[0].GetSubItemAt(e.X, e.Y).Bounds.Width;
                    eHeight = lsv.SelectedItems[0].GetSubItemAt(e.X, e.Y).Bounds.Height;
                    eIndex = lsv.SelectedItems[0].Index;
                    OnMouseDoubleClick();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("fcLisview control double click\n" + ex.ToString()); 
            }
        }
        public bool booProcFromData = true;
        public bool fcProcFromData
        {
            get
            {
                return booProcFromData;
            }
            set
            {
                booProcFromData = value;
            }
        }
        private bool booFlag = false;
        public bool booItemChecked = false;
        public string strItemCheckTag = "";
        private void lsv_ItemChecked(object sender, ItemCheckedEventArgs e)
        {

            strItemCheckTag = Convert.ToString(e.Item.Tag);
            if (booFlag == true)
            {
                OnItemChecked();
                booFlag = false;
            }
        }

        private void lsv_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (Convert.ToString(e.NewValue)== "Checked")
                booItemChecked = true;
            else
                booItemChecked = false;

            
        }
        private bool booShowTitle = true;
        public bool fcShowTitle
        {
            get
            {
                return booShowTitle;
            }
            set
            {
                booShowTitle = value;
                if (booShowTitle == true)
                {
                    lsv.View = View.Details;
                }
                else
                {
                    lsv.View = View.List;
                }
            }
        }

        private void lsv_Click(object sender, EventArgs e)
        {
            booFlag = true;
        }
        private bool booSort = true;
        public bool fcSort
        {
            get
            {
                return booSort;
            }
            set
            {
                booSort = value;
                if (booSort == true)
                {
                    cmbSort.Visible = true;
                    label1.Visible = true;
                    cmbSort.Show();
                }
                else 
                {
                    cmbSort.Visible = false;
                    label1.Visible = false;
                }
            }
        }

        private void cmbSort_SelectedValueChanged(object sender, EventArgs e)
        {

            try
            {
                //(cmbSort.SelectedItem as ComboBoxItem.Item).Value.ToString();
                string strSQL = this.strProc;
                strSQL += " N'" + cmbSort.SelectedItem.ToString() +"'";
                cmbSort.Text = cmbSort.SelectedItem.ToString();
                this.LoadData(strSQL, this.Conn);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ComboBox select - Sort \n" + ex.ToString());
            }
            
        }

        private void lsv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(System.Windows.Forms.Keys.Enter))
            {
                if (booCheckBox == false)
                {
                    strSelectItemCode = Convert.ToString(lsv.SelectedItems[0].Tag);
                    ListView.SelectedListViewItemCollection items = lsv.SelectedItems;
                    OnDoubleClick();
                }
            }
        }

       

        



    }
}
